package com.sunyard.client2.ui;

import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;

import javax.swing.JButton;
import javax.swing.JPanel;

public class ResultMessageBox extends JPanel {

	private static ResultMessageBox instance = new ResultMessageBox();

	public static ResultMessageBox getInstance() {
		return instance;
	}

	private Queue<String> messages = new LinkedList<String>();

	private TextArea ta = new TextArea("", 10, 50, TextArea.SCROLLBARS_BOTH);
	private int num = 1;

	/**
	 * Create the panel.
	 */
	public ResultMessageBox() {
		setLayout(new BorderLayout(0, 0));
		add(ta, BorderLayout.CENTER);
		JButton btnNewButton = new JButton("清除");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				messages.clear();
				num = 1;
				setMessage();
			}
		});
		add(btnNewButton, BorderLayout.EAST);
		showMessage("初始化成功");
	}

	public void showMessage(String message) {
		messages.add(message);
		if (messages.size() > 8) {
			messages.poll();
			num++;
		}
		setMessage();
	}

	private void setMessage() {
		StringBuilder str = new StringBuilder();
		Object[] ms = messages.toArray();
		for (int i = 0; i < ms.length; i++) {
			str.append("[").append(num + i).append("]--->").append(" ").append(ms[i]).append("\r\n");
		}
		str.append("                                    ");
		ta.setText(str.toString());
		this.updateUI();
	}
}
